Uec3.SaleBrowserWindow = Ext.extend(Ext.app.Module, {
   id: 'sale_browser',
   //type: 'common/notepad', 
   
   getPanel:function(){
     
		var ds_source_select = new Ext.data.JsonStore({
           url:Uec3.App.Constants.homeUrl+"/dictionary/findByVarGroup.do?group=part_source",
     		root: "",
     		autoLoad :true,
     		fields:[{name:'varIndex',type:'int'},{name:'varName',type:'string'}]
 		}); 
		
 		 var ds_sale_status_select = new Ext.data.JsonStore({
             url:Uec3.App.Constants.homeUrl+"/dictionary/findByVarGroup.do?group=sale_status",
             root: "",
             autoLoad:true,
             fields:[{name:'varIndex',type:'int'},{name:'varName',type:'string'}]
        }); 
        
 		var sale_browser_store = new Ext.data.JsonStore({
			url:Uec3.App.Constants.homeUrl+"/sale/saleBrowser.do",				
			remoteSort :true,
		    root: "root",
           idProperty: "id",
           totalProperty: "totalProperty", 
           pruneModifiedRecords:true,
           fields:[
             'codeOfSeller','statusName','amount','currentorName','creatorName',
             'submitterName','deliveryerName','memoOfSeller','status','creatorId','currentorId','id',
              {name:'partnerName',mapping:'partnerNameBySeller'},
              {name:'gmtSubmit',type:'date',dateFormat:'Y-m-d H:i:s'},
              {name:'gmtDelivery',type:'date',dateFormat:'Y-m-d H:i:s'},
              {name:'gmtCreate',type:'date',dateFormat:'Y-m-d H:i:s'},
              {name:'gmtRealDelivery',type:'date',dateFormat:'Y-m-d H:i:s'}
           ],
           listeners :{
              'beforeload':function(){
                 Ext.MessageBox.wait("正在加载数据...","提示");
              },
              'load':function(store,records,option){
              		Ext.each(records,function(record,index,records){                   	
	    	            var recordId= record.data.id;	    	        
		                Ext.get('browserSaleDetails_'+recordId).on('click',function(){
		             	    browserSaleDetails(recordId);
		                });
		                if(record.data.status == 0 && record.data.currentorId == record.data.creatorId){
		                    Ext.get('deleteSaleById_'+recordId).on('click',function(){
		             	       deleteSaleById(recordId);
		                    });  
		                }		                           	
                    });
              }
             
           }
		});
 		
 		var sale_browser_grid = new Ext.grid.GridPanel({
			     frame :false,
                 columnLines :true,
                 height:408,                   
                 enableHdMenu : false,
                 stripeRows: true,
                 store:sale_browser_store,                  
                 width : '100%',	   	   
  			     viewConfig:
  	   	           {
  	   	           	//autoFill:true,
  	                //forceFit:true,
  	                scrollOffset:20
  	               },	   	   
  	             autoScroll : true,
  	             //autoExpandColumn:'memo',
		         columns:[
		            new Ext.grid.RowNumberer(),
		            {header:'操作',renderer:addSaleBrowserOpertion,width:115},
		            {header:'单据号',dataIndex:'codeOfSeller',width:135,sortable:true},
		            {header:'卖方',dataIndex:'partnerName',width:130},			           
		            {header:'状态',dataIndex:'statusName',width:130},
		            {header:'金额(进价)',dataIndex:'amount',width:130},
		            {header:'当前处理人',dataIndex:'currentorName',width:130},
		            {header:'创建人',dataIndex:'creatorName',width:130},
		            {header:'创建时间',dataIndex:'gmtCreate',width:130,renderer:Ext.util.Format.dateRenderer('Y-m-d')},
		            {header:'提交人',dataIndex:'submitterName',width:130},
		            {header:'提交时间',dataIndex:'gmtSubmit',width:130,renderer:Ext.util.Format.dateRenderer('Y-m-d')},
		            {header:'审核人',dataIndex:'deliveryerName',width:130},
		            {header:'审核时间',dataIndex:'gmtDelivery',width:130,renderer:Ext.util.Format.dateRenderer('Y-m-d')},
		            {header:'发货时间',dataIndex:'gmtRealDelivery',width:130,renderer:Ext.util.Format.dateRenderer('Y-m-d')},
		            {header:'备注',dataIndex:'memoOfSeller',width:200}				            
		         ],
		         bbar: new Ext.PagingToolbar({
		            pageSize:8,
	                store:sale_browser_store,
	                displayInfo: true,
	                displayMsg: '第 {0} - {1} 条  共 {2} 条',
                    emptyMsg: "没有记录",                   
                    listeners:{
                        beforechange:function(){
                      		 Ext.MessageBox.wait("正在加载数据...","提示");	                     
                        },
                        change:function(){
                            Ext.MessageBox.hide();
                        }
                    }
		         })
		});
 		
		var view_panel=new Ext.FormPanel({
		      //autoScroll:true,
              frame:false,
              border:true,
		      items:[
		      	{		         
		          height:60,
		          layout:'hbox',
		          border:false,
		          layoutConfig: 
		             {     
		               padding:'5',
					   align:'middle'
                    },
		          items:[			             
		              {
		               xtype:'label',
		               text:'浏览销售单',
		               style: {
     					  font:'bold 30px arial,serif'
   					   }
		             },{
		             	xtype:'button',
		             	text: '切换到明细浏览方式',
		             	margins:'0 10',
		             	handler:function(){
		            	     redirectUrl("saleItem_browser");
		            	}
		             },{			                
		                xtype:'spacer',
                        flex:1
		             },{
                       xtype:'numberfield',
                       name:'codeOfSeller'                      
                    },{
                       xtype:'button',
                       text:'查找单据号',                   
                       margins:'0 10',
                       handler:searchByCode
                    },{
                       xtype:'button',
                       text:'高级查找',
                       handler:showSearchForm
                    },{
                       xtype:'spacer',
                       flex:1
                    },{
                       xtype:'button',
                       text: '新增销售出库单',
                       handler:function(){
                    	    redirectUrl("sale_add");
                       }
                    },{
                       xtype:'button',
                       text: '新增销售退货',
                       margins:'0 10',
                       handler:function(){
                             redirectUrl("saleReturn_edit");
                       }
                    }
		          ]
		      },sale_browser_grid			      	
		      ]
		});
		
		var advanced_search_form = new Ext.Panel({
		         labelSeparator : "：",
		         frame:true,		         
		         border:false,			    
		         items:[
		            {
		              xtype:'textfield',
				      width : 200,
		              fieldLabel:'单据号',
		              name:'codeOfSeller'
		            },{
		               xtype:'combo',
			           width : 200,
				       allowBlank : false,
					   name : 'status',
					   fieldLabel:'单据状态',
				       valueField:'varIndex',
                       displayField : 'varName',
	  				   mode : 'remote',
  				       hiddenName:'status',
  				       store : ds_sale_status_select,
  				       selectOnFocus : true,
  				       triggerAction : 'all',
  				       loadingText : '加载中...',
  				       editable:false,
  				       emptyText : '请选择'	 			     
		            },{
		               xtype:'textfield',
				       width : 200,
		               fieldLabel:'供货商',
		               name:'partnerName' 
		            },{
		               xtype:'datefield',
				       width : 200,
		               fieldLabel:'开始时间',
		               name:'gmtStart' ,
		               format:'Y-m-d'
		            },{
		               xtype:'datefield',
		               width : 200,
		               fieldLabel:'结束时间',
		               name:'gmtEnd',
		               format:'Y-m-d'
		            },{
		              xtype:'panel',
		              layout:'hbox',
		              border: false,
		              ctCls:'x-form-item',
		              items:[
		                 {
		                   xtype:'label',
		                   text:'进价(金额)：',
		                   margins:'4 0'
		                  // style:'font-size:15px'
		                   //ctCls:'x-form-item-label'
		                 },{
		                   xtype:'textfield',
		                   name:'minAmount',
		                   width:90,
		                   margins:'0 0 0 35'
		                 },{
		                    xtype:'label',
		                    text:'——'
		                 },{
		                    xtype:'textfield',
		                    name:'maxAmount', 
		                    width:90
		                 }
		              ]
		              
		            }
		         ],
		      buttons:[
		        { 
		         text:'查找',
				 handler:searchSaleByCondition
		        },{
		     	  text:'关闭',
		     	  handler:function(){	
		     		
		     	    win.hide();
		     	  }
		        }
		      ]
			
		});
		
		var win = new Ext.Window({
			title:'高级查找',
		    layout:'fit',
	    	width:380,
	    	closeAction:'hide',
	    	height:330,
			resizable :true,
			shadow : true,
			modal :true,
	    	closable:true,
	    	bodyStyle:'padding:5 5 5 5',
	    	animCollapse:true,
			items:[advanced_search_form]
	    });
	    
	    //跳转销售单明细编辑页面
	    function browserSaleDetails(saleId){		    
	        //openByUrl(Uec3.App.Constants.homeUrl+"/sale/tosale!tosaleItemEdit.do?queryParam="+saleId,'入库单');
	    	var desktop = Uec3.App.getDesktop();
	    	desktop.launchWindowByParam("sale_add",{id:saleId,winId:saleId});
	    }
	    
	    //删除销售单
	    function deleteSaleById(saleId){
	   	    var currentCount = sale_browser_store.getCount();
	    	Ext.Ajax.request({
            	url:Uec3.App.Constants.homeUrl+"/sale/toSale!deleteDraft.do",
            	params :{queryParam:saleId},
            	method :"post",
            	callback :function(options,success,response){
                    
           	},
            	success:function(response,options){
            		var resp = Ext.decode(response.responseText);
            		Ext.MessageBox.alert("提示",resp.msg); 
            		if(resp.success){
            			if(currentCount == 1){
            		    	sale_browser_grid.getBottomToolbar().movePrevious();
            			}else{
            		   	    sale_browser_store.reload();
            			}
            		}            		
            	},
            	failure:function(response,options){
               	Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
            	}
          });
	   }
	    
	    function addSaleBrowserOpertion(value, metadata, record, rowIndex, colIndex,store){	    	
	         var href0 = "<a style='font-size:12px' href='#' id=browserSaleDetails_"+record.data.id+">操作</a>";
	         var href1 = "<a  style='font-size:12px' href='#' id=deleteSaleById_"+record.data.id+">删除</a>";	
	         var href;
	         if(record.data.status == 0 && record.data.currentorId == record.data.creatorId){
	              href = href0+" | "+href1;
	         }else{
	              href = href0;
	         }
	         return href;
	    }
	   
	    function searchByCode(){
	      var value=view_panel.getForm().findField('codeOfSeller').getValue();
	      var queryParam=Ext.util.JSON.encode({codeOfSeller:value});		      
		  loadSale(queryParam);
	    }
	    
		function showSearchForm(){
			advanced_search_form.form.reset();
		    win.show();
		}
		
		function searchSaleByCondition(){
		   var queryParam =Ext.util.JSON.encode(advanced_search_form.getForm().getValues());		
		   loadSale(queryParam);
		   win.hide();
		}
		
		 function loadSale(queryParam){
			if(queryParam){
				sale_browser_store.baseParams = {start:0,limit:8,queryParam:queryParam};
			}else{
				sale_browser_store.baseParams = {start:0,limit:8};
			}   
		    sale_browser_store.load({callback:function(records,options,success){
	              if(success){
	              	 Ext.MessageBox.hide();
	                 var total=records.length;
	                 if(!total){
	                    Ext.MessageBox.alert("提示","未找到相关记录");
	                 }
	              }else{
	                 Ext.MessageBox.alert("提示","数据加载异常");
	              }
		    }});
	    }
	    
		 function redirectUrl(id,params){
		     	var desktop =Uec3.App.getDesktop();   
		 		desktop.launchWindowByParam(id,params); 	 		
	     }
		 
	 /*   init();
	    function init(){
	    	loadsale();
	    }*/
        return view_panel;
   },
	/**
	 * Create this modules window here.
	 */
   createWindow : function(){
      var desktop = this.app.getDesktop();
      var win = desktop.getWindow('sale_browser');
      if(!win){	
         win = desktop.createWindow({
            id: 'sale_browser',
            title: '浏览销售出库单',
            width: 1000,
            height: 500,
            minWidth:785,
            minHeight:500,
            iconCls: 'acc-icon',
            shim: false,
            animCollapse: false,
            constrainHeader: true,
            maximizable: true,
            maximized:true,
            taskbuttonTooltip: "浏览销售出库单",
            //layout: "fit",          
            items:[this.getPanel()]
         });
      }
      win.get(0).get(1).getBottomToolbar().doRefresh();//刷新列表 
      win.get(0).get(1).setHeight(win.getHeight()-90);
      win.show(); 
      win.on('resize',function(win,width,height){
      	  win.get(0).get(1).setHeight(height-92);
      });
    }
});